package servlet;

import dao.CartDAO;
import entity.Cart;
import org.bson.types.ObjectId;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "cartDeleteByUser",value = "/cart/userdelete")
public class CartDeleteByUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        ObjectId oid=new ObjectId(id);
        CartDAO cartDAO=new CartDAO();
        Cart cart=cartDAO.findById(oid);
        if(cart==null){
            resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Cart item not found");
            return;
        }
        ObjectId userId=cart.getUserId();


        int status = cartDAO.delCart(oid);
        /*Cart cart = cartDAO.findById(oid);
        System.out.println("获取"+cart.getUserId());*/
        if(status != 0){
            System.out.println("删除成功");
            resp.sendRedirect("/Mall/cart/userList?id="+userId);
            //resp.sendRedirect("/Mall/cart/list");
        }
    }
}
